package xian.woniuxy.be.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.be.entity.Customer;
import xian.woniuxy.be.entity.CustomerExample;
import xian.woniuxy.be.entity.ResultVO;
import xian.woniuxy.be.entity.Users;
import xian.woniuxy.be.service.ICustomerService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ljy
 * @time 2022/01/03 15:02:53
 */
@RestController
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @PostMapping("customer/save")
    public ResultVO save(@RequestBody Customer customer){
        if (customer.getCusName() == ""){
            return new ResultVO(501, "用户名不能为空", null);
        }
        if (customer.getUid() == null){
            return new ResultVO(502, "客户经理不能为空", null);
        }
        customerService.save(customer);
        return ResultVO.success("操作成功",null);
    }

    @PostMapping("customer/delete")
    public ResultVO delete(@RequestBody String cusId){
        if (!StringUtils.hasText(cusId)){
            throw new RuntimeException("客户ID为空");
        }
        customerService.delete(Integer.valueOf(cusId));
        return ResultVO.success("删除成功");
    }

    @PostMapping("customer/update")
    public ResultVO update(@RequestBody Customer customer){
        if (customer.getCusName() == ""){
            return new ResultVO(501, "用户名不能为空", null);
        }
        if (customer.getUid() == null){
            return new ResultVO(502, "客户经理不能为空", null);
        }
        customerService.update(customer);
        return ResultVO.success("修改成功");
    }

    @PostMapping("customer/findAll")
    public ResultVO findAll(){
        List<Customer> list = customerService.findAll();
        return ResultVO.success(list);
    }

    @PostMapping("customer/findOne")
    public ResultVO findOne(@RequestBody String cusId){
        if (!StringUtils.hasText(cusId)){
            throw new RuntimeException("客户ID为空");
        }
        Customer customer = customerService.findOne(Integer.valueOf(cusId));
        return ResultVO.success(customer);
    }

    @PostMapping("customer/selectFromname")
    public ResultVO selectByName(@RequestBody String theName){
        List<Customer> list = new ArrayList<>();
        if (!StringUtils.hasText(theName)){
            list = customerService.findAll();
        }
        list = customerService.selectFromname(theName);
        return ResultVO.success(list);
    }
}
